home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / allison / tstr.cpp < prev   
C/C++ Source or Header  |  1993-10-10  |  1KB  |  46 lines

  1. LISTING 3 - Illustrates the String Class
  2. // tstr.cpp:     Test the string class
  3.  
  4. #include <iostream.h>
  5. #include "string.hpp"
  6.  
  7. main()
  8. {
  9.     size_t pos;
  10.     string s1("Hello"), s2 = ", there, you fool!  ", s3;
  11.  
  12.     // Concatenation
  13.     cout << "s1 + s2 == " << s1+s2 << endl;
  14.     s1 += s2;
  15.     cout << "Now s1 == " << s1 << endl;
  16.  
  17.     // Searching
  18.     if ((pos = s1.find("o")) != NPOS)
  19.         cout << "find(\"o\"): " << pos << endl;
  20.     cout << "s1.substr(4,10) == " << s1.substr(4,10) << endl;
  21.     if ((pos = s1.find_first_of("abcde")) != NPOS)
  22.         cout << "find_first_of(abcde): " << pos << endl;
  23.     if ((pos = s1.find_first_not_of("abcde")) != NPOS)
  24.         cout << "find_first_not_of(abcde): " << pos << endl;
  25.  
  26.     // Subscripting
  27.     s3 = "This is a char* constant";
  28.     cout << "s3[0] == " << s3.get_at(0) << endl;
  29.     s3.put_at(3,'3');
  30.     s3.put_at(s3.length(),'!');
  31.     cout << "Now s3 == " << s3 << endl;
  32.  
  33.     return 0;
  34. }
  35.  
  36. /* OUTPUT:
  37. s1 + s2 == Hello, there, you fool!  
  38. Now s1 == Hello, there, you fool!  
  39. find("o"): 4
  40. s1.substr(4,10) == o, there, 
  41. find_first_of(abcde): 1
  42. find_first_not_of(abcde): 0
  43. s3[0] == T
  44. Now s3 == Thi3 is a char* constant!
  45. */
  46.